﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TFXK.Model;
using TFXK.BLL;
using TFXK.Common;
using log4net;

public partial class admin_HotelManage_Default : BasePage
{
    private static readonly ILog log = LogManager.GetLogger(typeof(admin_HotelManage_Default));
    private static readonly HotelBLL bll = new HotelBLL();
    private static readonly CategoryBLL cbll = new CategoryBLL();

    public DataSet ds = new DataSet();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 设置下拉列表分类信息
    public void SetDDLView()
    {
        DevExpress.Web.ASPxEditors.ListEditItem listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("未选择", 0);
        DataSet ds = cbll.GetNextNodeByCode("hotel_zt");
        ddlMsgType.DataSource = ds.Tables[0].DefaultView;
        ddlMsgType.TextField = "title";
        ddlMsgType.ValueField = "id";
        ddlMsgType.DataBind();
        ddlMsgType.Items.Insert(0, listItemQXZ);
        ddlMsgType.SelectedIndex = 0;

        listItemQXZ = new DevExpress.Web.ASPxEditors.ListEditItem("未选择", 0);
        ds = cbll.GetNextNodeByCode("hotel_city");
        this.ddlCity.DataSource = ds.Tables[0].DefaultView;
        ddlCity.TextField = "title";
        ddlCity.ValueField = "id";
        ddlCity.DataBind();
        ddlCity.Items.Insert(0, listItemQXZ);
        ddlCity.SelectedIndex = 0;
    }
    #endregion

    #region 分页绑定
    private void InitData(int PageIndex, string title, int typeid,int cityid)
    {
        this.Title = "列表";    // 设置标题

        DataSet ds = new DataSet();
        int recordcount = 0; //获取总条数

        ds = bll.GetList(AspNetPager1.PageSize, PageIndex, title, typeid, cityid, out recordcount);

        AspNetPager1.RecordCount = recordcount;
        this.rptBind.DataSource = ds.Tables[0].DefaultView;
        this.rptBind.DataBind();
    }
    #endregion

    #region 分页控件加载
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                SetDDLView();
                string name = string.Empty;
                int typeid = 0;
                int cityid = 0;
                if (!string.IsNullOrEmpty(Request.QueryString["title"]))
                {
                    name = Server.UrlDecode(StringUtil.HtmlEncode(Request.QueryString["title"]));
                }
                if (!string.IsNullOrEmpty(Request.QueryString["typeid"]))
                {
                    typeid = Int32.Parse(Server.UrlDecode(StringUtil.HtmlEncode(Request.QueryString["typeid"])));
                }
                if (!string.IsNullOrEmpty(Request.QueryString["cityid"]))
                {
                    cityid = Int32.Parse(Server.UrlDecode(StringUtil.HtmlEncode(Request.QueryString["cityid"])));
                }
                InitData(e.NewPageIndex, name, typeid,cityid);
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 删除
    protected void ibtnDel_OnCommand(object sender, CommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandName);
        try
        {
            // 防止网页重复提交
            if (this.CheckRefreshed1.isRefreshed == false)
            {
                bll.Delete(id);
                Msg.Show("删除成功!");
            }
            InitData(1, "", 0,0); //重新绑定
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 多选删除
    protected void lbtnDelAll_Click(object sender, EventArgs e)
    {
        try
        {
            // 防止网页重复提交
            if (this.CheckRefreshed1.isRefreshed == false)
            {
                string id = string.Empty;
                bool flg = false;

                foreach (RepeaterItem rptItem in this.rptBind.Items)
                {
                    HtmlInputCheckBox chkDelete = rptItem.FindControl("chkSelected") as HtmlInputCheckBox;
                    if (chkDelete.Checked == true)
                    {
                        id = chkDelete.Value.ToString();
                        bll.Delete(Int32.Parse(id));
                        //    pcbll.DeleteByParent(str);
                        flg = true;
                    }
                }

                if (flg)
                {
                    Msg.Show("删除成功!");
                }
                else
                {
                    Msg.Show("请选择要删除的记录!");
                }
            }
            InitData(1, "", 0,0); //重新绑定
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 修改
    protected void lbtnModify_Click(object sender, EventArgs e)
    {
        try
        {
            string id = string.Empty;

            foreach (RepeaterItem rptItem in this.rptBind.Items)
            {
                HtmlInputCheckBox chkDelete = rptItem.FindControl("chkSelected") as HtmlInputCheckBox;
                if (chkDelete.Checked == true)
                {
                    id = chkDelete.Value.ToString();
                    Response.Redirect("Action.aspx?type=modify&id=" + id, false);
                    return;
                }
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 搜索按钮
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            string name = this.txtSearchName.Text;
            string typeid = this.ddlMsgType.SelectedItem.Value.ToString();
            string cityid = this.ddlCity.SelectedItem.Value.ToString();
            Response.Redirect("Default.aspx?title=" + name + "&typeid=" + typeid + "&cityid=" + cityid, false);
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }
    }
    #endregion

    #region 设置分类信息
    public string GetCategoryName(object obj)
    {
        CategoryBLL cbll = new CategoryBLL();
        return cbll.GetModel(int.Parse(obj.ToString())).title;
    }
    #endregion
}
